package com.gitee.qdbp.general.common.biz.settings.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSONException;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.ResultCode;
import com.gitee.qdbp.general.common.api.settings.model.SystemConfigCoreBean;
import com.gitee.qdbp.general.common.api.settings.service.ISystemConfigItemQueryer;
import com.gitee.qdbp.general.common.enums.ConfigDataType;
import com.gitee.qdbp.general.common.enums.ConfigScene;
import com.gitee.qdbp.general.common.enums.ConfigValueType;
import com.gitee.qdbp.tools.utils.ConvertTools;
import com.gitee.qdbp.tools.utils.VerifyTools;
import libs.fastjson.com.alibaba.fastjson.JSON;

/**
 * 系统配置业务处理类
 *
 * @author zhh
 * @version 180506
 */
@Service
@Transactional(readOnly = true)
public class SystemConfigItemQueryer extends SystemConfigBaseQueryer implements ISystemConfigItemQueryer {

    /** 日志对象 **/
    private static final Logger log = LoggerFactory.getLogger(SystemConfigItemQueryer.class);

    public SystemConfigItemQueryer() {
        super(ConfigDataType.ITEM);
    }

    @Override
    public String findTextValue(ConfigScene scene, String tenantCode, String key) throws ServiceException {
        SystemConfigCoreBean bean = findByKey(scene, tenantCode, key);
        if (bean == null) {
            return null;
        }
        if (!ConfigValueType.text.name().equals(bean.getOptions(true).getValueType())) {
            return null;
        }
        return bean.getConfigValue();

    }

    @Override
    public Double findNumberValue(ConfigScene scene, String tenantCode, String key)
            throws ServiceException, NumberFormatException {
        SystemConfigCoreBean bean = findByKey(scene, tenantCode, key);
        if (bean == null) {
            return null;
        }
        if (!ConfigValueType.number.name().equals(bean.getOptions(true).getValueType())) {
            return null;
        }
        String configValue = bean.getConfigValue();
        if (VerifyTools.isBlank(configValue)) {
            return null;
        }
        return ConvertTools.toDouble(configValue);
    }

    @Override
    public String findEnumValue(ConfigScene scene, String tenantCode, String key) throws ServiceException {
        SystemConfigCoreBean bean = findByKey(scene, tenantCode, key);
        if (bean == null) {
            return null;
        }
        if (!ConfigValueType.enums.name().equals(bean.getOptions(true).getValueType())) {
            return null;
        }
        return bean.getConfigValue();
    }

    @Override
    public <T> T findObjectValue(ConfigScene scene, String tenantCode, String key, Class<T> clazz)
            throws ServiceException {
        SystemConfigCoreBean bean = findByKey(scene, tenantCode, key);
        if (bean == null) {
            return null;
        }
        if (!ConfigValueType.object.name().equals(bean.getOptions(true).getValueType())) {
            return null;
        }
        String configValue = bean.getConfigValue();
        if (VerifyTools.isBlank(configValue)) {
            return null;
        }
        try {
            return JSON.parseObject(configValue, clazz);
        } catch (JSONException e) {
            log.error("Parse json error, scene={}, key={}, configValue={}", scene, key, configValue, e);
            throw new ServiceException(ResultCode.SERVER_INNER_ERROR, e);
        }
    }

}
